package com.eclink.asset.hardware.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eclink.asset.domain.query.WarehouseQuery;
import com.eclink.asset.hardware.domain.dao.Hardware;
import com.eclink.asset.hardware.domain.dto.DeviceDTO;
import com.eclink.asset.hardware.mapper.HardwareMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 公司Service
 *
 * @author chenshixian
 */
@Service
public class HardwareService {

    @Resource
    private HardwareMapper mapper;

    @Resource
    private RfidService rfidService;

    /**
     * 查询
     *
     * @param id ID
     * @return 结果
     */
    public Hardware selectById(Long id) {
        return mapper.selectById(id);
    }

    /**
     * 查询仓库名称
     *
     * @param id 仓库主键
     * @return 仓库
     */
    public String getNameById(Long id) {
        Hardware warehouse = selectById(id);
        if (warehouse == null) {
            return null;
        }
        return warehouse.getName();
    }

    /**
     * 查询仓库列表
     *
     * @param query 查询条件
     * @return 仓库
     */
    public List<Hardware> selectList(WarehouseQuery query) {

        QueryWrapper<Hardware> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        String warehouseNo = query.getWarehouseNo();
        if (!StringUtils.isEmpty(warehouseNo)) {
            qw.eq("warehouse_no", warehouseNo);
        }
        String warehouseNameLike = query.getWarehouseNameLike();
        if (!StringUtils.isEmpty(warehouseNameLike)) {
            qw.like("warehouse_name", warehouseNameLike);
        }
        return mapper.selectList(qw);
    }

    /**
     * 查询所有
     *
     * @return 仓库
     */
    public List<Hardware> selectListAll() {

        QueryWrapper<Hardware> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        return mapper.selectList(qw);
    }

    /**
     * 查询所有
     *
     * @return 仓库
     */
    public List<Hardware> selectListAllAutoStart() {

        LambdaQueryWrapper<Hardware> qw = new LambdaQueryWrapper<>();
        qw.eq(Hardware::getDelFlag,0);
        qw.eq(Hardware::getAutoStartFlag,1);
        return mapper.selectList(qw);
    }

    /**
     * 新增仓库
     *
     * @param entity 仓库
     * @return 结果
     */
    public int insert(Hardware entity) {
        entity.setDelFlag(0);
        entity.setCreateTime(LocalDateTime.now());
        int res = mapper.insert(entity);
        DeviceDTO dto = BeanUtil.copyProperties(entity, DeviceDTO.class);
        rfidService.publish(dto);
        return res;
    }

    /**
     * 关闭
     *
     * @param id ID
     * @return 结果
     */
    public void start(Long id) {
        rfidService.start(id);
    }

    /**
     * 关闭
     *
     * @param id ID
     * @return 结果
     */
    public void shutdown(Long id) {
        rfidService.shutdown(id);
    }

    /**
     * 修改仓库
     *
     * @param warehouse 仓库
     * @return 结果
     */
    public int update(Hardware warehouse) {
        return mapper.updateById(warehouse);
    }


    /**
     * 删除仓库信息
     *
     * @param id 仓库主键
     * @return 结果
     */
    public int deleteById(Long id) {
        return mapper.deleteById(id);
    }

    public void deleteByIds(Long[] ids) {
        for(Long id : ids) {
            deleteById(id);
        }
    }

    /**
     * 根据主键集合查询仓库列表
     *
     * @param ids 主键集合
     * @return 仓库列表
     */
    public List<Hardware> selectByIdIn(Collection<Long> ids) {
        // 如果主键集合为空，直接返回空集合
        if (ids == null || ids.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<Hardware> qw = new QueryWrapper<>();
        qw.in("id",ids);
        return mapper.selectList(qw);
    }
}
